package com.meilele.controller.czh;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/files")
public class FilesController {
    @RequestMapping("/download/{name}")
	public void download(@PathVariable("name") String name, HttpServletResponse response){
		response.setHeader("Content-Disposition", "attachment;filename="+name);
		show(name, response);
	}
	@RequestMapping("/show")
	public void show(String name,HttpServletResponse response){
		File file=new File(Contants.IMAGE_PATH+name);
		if(file.exists()){
			try {
				FileInputStream fis=new FileInputStream(file);
				OutputStream os=response.getOutputStream();
				byte[] by=new byte[1024];
				int len=0;
				while((len=fis.read(by))!=-1){
					os.write(by, 0, len);
				}
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
